import { Request, Response } from 'express'
import { User } from '../models/User'

export const rechargeBalance = async (req: Request, res: Response) => {
  try {
    const { amount } = req.body
    const user = await User.findByIdAndUpdate(
      req.user!.id,
      { $inc: { balance: amount } },
      { new: true }
    )

    res.json({
      message: '充值成功',
      balance: user?.balance,
    })
  } catch (error) {
    res.status(500).json({ message: '服务器错误' })
  }
}

export const getBalance = async (req: Request, res: Response) => {
  try {
    const user = await User.findById(req.user!.id)
    res.json({ balance: user?.balance })
  } catch (error) {
    res.status(500).json({ message: '服务器错误' })
  }
}
